package com.witdata.common.service.impl;

import com.witdata.common.util.MinioUtils;
import com.witdata.common.service.ImageService;
import io.minio.ObjectWriteResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

@Service
@RequiredArgsConstructor
public class ImageServiceImpl implements ImageService {
    private final MinioUtils minioUtils;

    @Override
    public String uploadAvatar(Long userId, String avatar) {
        String url = "/avatar/" + userId;
        return minioUtils.uploadImage("witdata", avatar, url);
    }

    @Override
    public void uploadImage(MultipartFile image, String url, String name) {
        String fileName = image.getOriginalFilename();
        if (StringUtils.hasLength(name)) {
            fileName =  name;
        }
        ObjectWriteResponse objectWriteResponse = minioUtils.uploadFile(image, url, fileName);
    }
}
